<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<link href="${base}/resources/admin/css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${base}/resources/admin/js/jquery.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/common.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/list.js"></script>
<script type="text/javascript">
    $().ready(function() {
        [@flash_message /]

		var $disableButton = $("#disableButton");
        var $enableButton = $("#enableButton");
        var $selectAll = $("#selectAll");
        var $ids = $("#listTable input[name='ids']");
        var $contentRow = $("#listTable tr:gt(0)");

        $("#searchPropertyOption").children("li").find("a.current").trigger("click");

        // 全选
        $selectAll.click( function() {
            var $this = $(this);
            var $enabledIds = $("#listTable input[name='ids']:enabled");
            if ($this.prop("checked")) {
                $enabledIds.prop("checked", true);
                if ($enabledIds.filter(":checked").size() > 0) {
                    $disableButton.removeClass("disabled");
                    $contentRow.addClass("selected");
                } else {
                    $disableButton.addClass("disabled");
                }
            } else {
                $enabledIds.prop("checked", false);
                $disableButton.addClass("disabled");
                $contentRow.removeClass("selected");
            }
        });

        // 选择
        $ids.click( function() {
            var $this = $(this);
            if ($this.prop("checked")) {
                $this.closest("tr").addClass("selected");
                $disableButton.removeClass("disabled");
            } else {
                $this.closest("tr").removeClass("selected");
                if ($("#listTable input[name='ids']:enabled:checked").size() > 0) {
                    $disableButton.removeClass("disabled");
                } else {
                    $disableButton.addClass("disabled");
                }
            }
        });

        // 禁用
        $disableButton.click( function() {
            var $this = $(this);
            if ($this.hasClass("disabled")) {
                return false;
            }
            var $checkedIds = $("#listTable input[name='ids']:enabled:checked");
            $.dialog({
                type: "warn",
                content: "确认禁用",
                ok: message("admin.dialog.ok"),
                cancel: message("admin.dialog.cancel"),
                onOk: function() {
                    $.ajax({
                        url: "disable",
                        type: "POST",
                        data: $checkedIds.serialize(),
                        dataType: "json",
                        cache: false,
                        success: function(message) {
                            $.message(message);
                            if (message.type == "success") {
//                                setTimeout(function() {
//                                    location.reload(true);
//                                }, 2000);
								$checkedIds.each(function (i, obj) {
									var id = $(obj).data("index");

                                    var tdStatus = $(obj).parent().parent().find(".status");
                                    tdStatus.empty();
                                    tdStatus.html('<span class="red">用户不可用<\/span>');

                                    var tdOpration = $(obj).parent().parent().find(".operation");
                                    tdOpration.empty();
                                    tdOpration.append('<a href="edit?id='+id+'">[编辑]</a><a href="#" onclick="enableUser(\''+id+'\',this)" >[启用]<\/a>');

                                    $(obj).attr("disabled","disabled");
                                    $(obj).attr("title","已被禁用");
                                });
                            }
                            $disableButton.addClass("disabled");
                            $selectAll.prop("checked", false);
                            $checkedIds.prop("checked", false);
                        }
                    });
                }
            });
        });

    });

    function enableUser(id, obj) {
        $.dialog({
            type: "warn",
            content: "确认启用",
            ok: message("admin.dialog.ok"),
            cancel: message("admin.dialog.cancel"),
            onOk: function () {
                $.ajax({
                    url: "enable?ids="+id,
                    type: "POST",
                    dataType: "json",
                    cache: false,
                    success: function(message) {
                        $.message(message);
                        if (message.type == "success") {
                            var tdStatus = $(obj).parent().parent().find(".status");
                            tdStatus.empty();
                            tdStatus.html('<span class="green">正常<\/span>');
                            $(obj).parent().append('<a href="#" onclick="disableUser(\''+id+'\',this)" >[禁用]<\/a>');

                            var checkbox = $(obj).parent().parent().find(".checkbox");
                            checkbox.removeAttr("disabled");
                            checkbox.removeAttr("title");

                            $(obj).remove();
                        }
                    }
                });
            }
        });
    }

    function disableUser(id, obj) {
        $.dialog({
            type: "warn",
            content: "确认禁用",
            ok: message("admin.dialog.ok"),
            cancel: message("admin.dialog.cancel"),
            onOk: function () {
                $.ajax({
                    url: "disable?ids="+id,
                    type: "POST",
                    dataType: "json",
                    cache: false,
                    success: function(message) {
                        $.message(message);
                        if (message.type == "success") {
                            var tdStatus = $(obj).parent().parent().find(".status");
                            tdStatus.empty();
                            tdStatus.html('<span class="red">用户不可用</span>');
                            $(obj).parent().append('<a href="#" onclick="enableUser(\''+id+'\',this)" >[启用]<\/a>');

                            var checkbox = $(obj).parent().parent().find(".checkbox");
							console.log(checkbox);
                            checkbox.attr("disabled","disabled");
                            checkbox.attr("title","已被禁用");

                            $(obj).remove();
            			}
                    }
                });

            }
        });
    }
</script>
</head>
<body class="list" >
  [#-- 用户管理--]
  <form id="listForm" action="list" method="get">
	  <div class="path">
		  <a href="${base}/admin/index">${message("admin.path.index")}</a> &raquo;
		  ${message("admin.admin.list")}<span>(${message("admin.page.total", pager.total)})</span>

		  <div class="menuWrap searchWrap">
			  <div class="search">
				  <span id="searchPropertySelect" class="arrow"> 选择字段 &nbsp;</span>
				  <input type="text" id="searchValue" name="searchValue" value="${pager.pageable.searchValue}" maxlength="200" />
				  <button type="submit">&nbsp;</button>
			  </div>
			  <div class="popupMenu">
				  <ul id="searchPropertyOption">
					  <li>
						  <a href="javascript:;"[#if pager.pageable.searchProperty == "su.login_name"] class="current"[/#if] val="su.login_name">用户名</a>
					  </li>
					  <li>
						  <a href="javascript:;"[#if pager.pageable.searchProperty == "su.name"] class="current"[/#if] val="su.name">姓名</a>
					  </li>
				  </ul>
			  </div>
		  </div>
	  </div>

		<div class="bar">
			<a href="add" class="iconButton">
				<span class="addIcon">&nbsp;</span>增加
			</a>
			<div class="buttonWrap">
				<a href="javascript:;" id="disableButton" class="iconButton disabled">
					<span class="deleteIcon">&nbsp;</span>禁用
				</a>
				<div class="menuWrap">
					<a href="javascript:;" id="pageSizeSelect" class="button">
						每页显示<span class="arrow">&nbsp;</span>
					</a>
					<div class="popupMenu">
						<ul id="pageSizeOption">
							<li>
								<a href="javascript:;"[#if pager.pageable.pageSize == 10] class="current"[/#if] val="10">10</a>
							</li>
							<li>
								<a href="javascript:;"[#if pager.pageable.pageSize == 20] class="current"[/#if] val="20">20</a>
							</li>
							<li>
								<a href="javascript:;"[#if pager.pageable.pageSize == 50] class="current"[/#if] val="50">50</a>
							</li>
							<li>
								<a href="javascript:;"[#if pager.pageable.pageSize == 100] class="current"[/#if] val="100">100</a>
							</li>
						</ul>
					</div>
				</div>
			</div>

		</div>
		<table id="listTable" class="list">
			<tr>
				<th class="check" >
					<input type="checkbox" id="selectAll" />
				</th>
				<th>
					<a href="javascript:;" class="sort" name="su.login_name">用户名</a>
				</th>
				<th>
					<a href="javascript:;" name="su.email">电子邮件</a>
				</th>
				<th>
					<a href="javascript:;" class="sort" name="su.name">姓名</a>
				</th>
				<th>
					<a href="javascript:;" class="sort" name="su.deptname">部门</a>
				</th>
				
				<th width="150">
					<span>状态</span>
				</th>
				<th>
					<a href="javascript:;" class="sort" name="su.create_date">创建日期</a>
				</th>
				<th>
					<span>操作</span>
				</th>
			</tr>
			[#list pager.content as admin]
				<tr>
					<td>
						<input data-index="${admin.id}" type="checkbox" class="checkbox" name="ids" [#if !admin.is_enabled] title="已被禁用" disabled="disabled"[#else] value="${admin.id}"[/#if]  />
					</td>
					<td>
						${admin.login_name}
					</td>
					<td>
						${admin.email!'-'}
					</td>
					<td>
						${admin.name}
					</td>
					<td>
						${admin.deptname!'-'}
					</td>
					<td class="status">
						[#if !admin.is_enabled]
							<span class="red">用户不可用</span>
						[#elseif admin.is_locked]
							<span class="red"> 用户被锁定 </span>
						[#else]
							<span class="green">正常</span>
						[/#if]
					</td>
					<td>
						<span >${admin.create_date?string("yyyy-MM-dd HH:mm:ss")}</span>
					</td>
					<td class="operation">
						<a href="edit?id=${admin.id}">[编辑]</a>

						[#if !admin.is_enabled]
						<a href="#" onclick="enableUser('${admin.id}',this)" >[启用]</a>
						[#else]
						<a href="#" onclick="disableUser('${admin.id}',this)" >[禁用]</a>
						[/#if]
					</td>
				</tr>
			[/#list]
		</table>

        [#if (pager.content?size > 0)]
			[@pagination pageNumber = pager.pageable.pageNumber totalPages = pager.pageable.totalPages]
				[#include "/admin/include/pagination.html"]
			[/@pagination]
		[#else ]
            <div class="norecord">
                <span>没有记录!</span>
            </div>
        [/#if]
	</form>
</body>
</html>